<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="所在供应商" prop="suppliername">
        <el-input
          v-model="queryParams.suppliername"
          placeholder="请输入所在供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入车辆品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车辆类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车身颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入车身颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicle:vehicle:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vehicle:vehicle:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vehicle:vehicle:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vehicle:vehicle:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="expand" align="center">
        <template slot-scope="scope">
          <el-tag style="font-size: 13px;"> 生产日期：{{ scope.row.manufacturedate }}</el-tag> ||
          <el-tag style="font-size: 13px;">
            发动机号：{{ scope.row.enginenumber }}
          </el-tag > ||
          <el-tag style="font-size: 13px;">
            车架号：{{ scope.row.vin }}
          </el-tag > ||

          <el-tag style="font-size: 13px;">
            核载人数：{{ scope.row.seatingcapacity }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            发动机类型：{{ scope.row.enginetype }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            排量：{{ scope.row.enginedisplacement }}
          </el-tag>
          <br><br>
          <el-tag style="font-size: 13px;">
            功率：{{ scope.row.power }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            车轴数量：{{ scope.row.axlesnumber }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            车轴距离：{{ scope.row.wheelbase }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            前轮距：{{ scope.row.fronttrack }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            后轮距：{{ scope.row.reartrack }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            满载总质量：{{ scope.row.grossvichcleweight }}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            汽车整备质量：{{ scope.row.curbweight }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所在供应商" align="center" prop="suppliername"/>
      <el-table-column label="车辆品牌" align="center" prop="brand"/>
      <el-table-column label="车辆类型" align="center" prop="model"/>
      <el-table-column label="车身颜色" align="center" prop="color"/>
      <el-table-column label="创建时间" align="center" prop="createtime"/>
      <el-table-column label="更新时间" align="center" prop="modifytime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicle:vehicle:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicle:vehicle:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>

          <el-col :span="12">
            <el-form-item label="所在供应商" prop="supplierid">
              <el-select v-model="form.supplierid" placeholder="请选择" filterable>
                <el-option
                  v-for="(item,index) in suppliersoptions"
                  :key="item.index"
                  :label="item.suppliername"
                  :value="item.supplierid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车身颜色" prop="color">
              <el-input v-model="form.color" placeholder="请输入车身颜色"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="车辆品牌" prop="brand">
              <el-input v-model="form.brand" placeholder="请输入车辆品牌"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆类型" prop="model">
              <el-input v-model="form.model" placeholder="请输入车辆类型"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="进价" prop="primeprice">
              <el-input v-model="form.primeprice" placeholder="请输入进价"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="manufacturedate">
              <el-input v-model="form.manufacturedate" placeholder="请输入生产日期"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发动机号" prop="enginenumber">
              <el-input v-model="form.enginenumber" placeholder="请输入发动机号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车架号" prop="vin">
              <el-input v-model="form.vin" placeholder="请输入车架号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="汽车整备质量" prop="curbweight">
              <el-input v-model="form.curbweight" placeholder="请输入汽车整备质量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核载人数" prop="seatingcapacity">
              <el-input v-model="form.seatingcapacity" placeholder="请输入核载人数"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发动机类型" prop="enginetype">
              <el-input v-model="form.enginetype" placeholder="请输入发动机类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排量" prop="enginedisplacement">
              <el-input v-model="form.enginedisplacement" placeholder="请输入排量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="功率" prop="power">
              <el-input v-model="form.power" placeholder="请输入功率"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车轴数量" prop="axlesnumber">
              <el-input v-model="form.axlesnumber" placeholder="请输入车轴数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="车轴距离" prop="wheelbase">
              <el-input v-model="form.wheelbase" placeholder="请输入车轴距离"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="满载总质量" prop="grossvichcleweight">
              <el-input v-model="form.grossvichcleweight" placeholder="请输入满载总质量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="前轮距" prop="fronttrack">
              <el-input v-model="form.fronttrack" placeholder="请输入前轮距"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后轮距" prop="reartrack">
              <el-input v-model="form.reartrack" placeholder="请输入后轮距"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle} from "@/api/vehicle/vehicle";
import {getlistSuppliers} from "@/api/supplier/supplier";

export default {
  name: "Vehicle",
  data() {
    return {
      //查询供应商
      suppliersoptions: [],
      //查询车辆品牌信息
      brandsoptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆管理表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        suppliername: null,
        warehousename: null,
        customername: null,
        salespersonname: null,
        brand: null,
        model: null,
        color: null,
        manufacturedate: null,
        enginenumber: null,
        vin: null,
        primeprice: null,
        price: null,
        seatingcapacity: null,
        enginetype: null,
        enginedisplacement: null,
        power: null,
        axlesnumber: null,
        wheelbase: null,
        fronttrack: null,
        reartrack: null,
        grossvichcleweight: null,
        curbweight: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /* status: [
           { required: true, message: "车辆状态标记", trigger: "blur" }
         ], */
        supplierid: [
          {required: true, message: "所在供应商ID不能为空", trigger: "blur"}
        ],
        /* warehouseid: [
           { required: true, message: "所在仓库ID不能为空", trigger: "blur" }
         ],
         customerid: [
           { required: true, message: "客户ID不能为空", trigger: "blur" }
         ],
         salespersonid: [
           { required: true, message: "销售员ID不能为空", trigger: "blur" }
         ], */
        brand: [
          {required: true, message: "车辆品牌不能为空", trigger: "blur"}
        ],
        model: [
          {required: true, message: "车辆类型不能为空", trigger: "blur"}
        ],
        color: [
          {required: true, message: "车身颜色不能为空", trigger: "blur"}
        ],
        manufacturedate: [
          {required: true, message: "生产日期不能为空", trigger: "blur"}
        ],
        enginenumber: [
          {required: true, message: "发动机号不能为空", trigger: "blur"}
        ],
        vin: [
          {required: true, message: "车架号不能为空", trigger: "blur"}
        ],
        primeprice: [
          {required: true, message: "进价不能为空", trigger: "blur"}
        ],
        /* price: [
           { required: true, message: "售价不能为空", trigger: "blur" }
         ], */
        seatingcapacity: [
          {required: true, message: "核载人数不能为空", trigger: "blur"}
        ],
        enginetype: [
          {required: true, message: "发动机类型不能为空", trigger: "blur"}
        ],
        enginedisplacement: [
          {required: true, message: "排量不能为空", trigger: "blur"}
        ],
        power: [
          {required: true, message: "功率不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
   /* this.getListBrands(); */
    this.getListSupplier();
  },
  methods: {
    //查询供应商
    getListSupplier() {
      getlistSuppliers().then(response => {
        this.suppliersoptions = response.data;
      })
    },
    //查询车辆品牌
   /* getListBrands() {
      getBrands().then(response => {
        this.brandsoptions = response.data;
      });
    }, */
    /** 查询车辆管理列表 */
    getList() {
      this.loading = true;
      listVehicle(this.queryParams).then(response => {
        console.info(response.rows)
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleid: null,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        brand: null,
        model: null,
        color: null,
        manufacturedate: null,
        enginenumber: null,
        vin: null,
        primeprice: null,
        price: null,
        createtime: null,
        modifytime: null,
        deleted: null,
        seatingcapacity: null,
        enginetype: null,
        enginedisplacement: null,
        power: null,
        axlesnumber: null,
        wheelbase: null,
        fronttrack: null,
        reartrack: null,
        grossvichcleweight: null,
        curbweight: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vehicleid = row.vehicleid || this.ids
      getVehicle(vehicleid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleid != null) {
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleids = row.vehicleid || this.ids;
      this.$modal.confirm('是否确认删除车辆管理编号为"' + vehicleids + '"的数据项？').then(function () {
        return delVehicle(vehicleids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicle/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
